<?php

require_once __DIR__ . '/../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Clearing\ClearingService;
use JLPay\Models\Clearing\WithdrawQueryRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

/**
 * 清算 - 提现查询功能使用示例
 */

// =================== 工具函数 =====================
/**
 * 创建配置对象
 */
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
/**
 * 构建提现查询请求
 */
function buildWithdrawQueryRequest(): WithdrawQueryRequest
{
    $request = new WithdrawQueryRequest();
    
    // 设置请求参数
    $request->setOrgNo("123456")
        ->setMerchNo("849584358120018")
        ->setOutSettleId("1234567890128");
    
    return $request;
}

// =================== 示例函数 =====================
/**
 * 提现查询示例
 */
function withdrawQueryDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行提现查询示例");
    
    // 创建配置对象和清算服务
    $config = createConfig();
    $clearingService = new ClearingService($config);
    
    // 创建提现查询请求
    $request = buildWithdrawQueryRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $clearingService, 
        $request, 
        "提现查询",
        function($service, $request) {
            return $service->queryWithdraw($request);
        }
    );

    // 处理响应结果
    if ($response) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
}

// =================== 主程序 =====================
if (php_sapi_name() === 'cli') {
    $logger = Logger::getInstance();
    
    // 设置日志级别为INFO
    $logger->setLevel('INFO');
    
    $logger->info(str_repeat("=", 50));
    $logger->info("===== 提现查询示例 开始 =====");
    $logger->info(str_repeat("=", 50));
    withdrawQueryDemo();
    $logger->info(str_repeat("=", 50));
    $logger->info("===== 提现查询示例执行完毕 =====");
    $logger->info(str_repeat("=", 50));
} 